package com.ctshk.common.utils;

import com.itextpdf.text.DocumentException;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 中港通巴士接口工具类
 */
public class HktBusUtil {
    private static final String url = "http://113.98.201.46:8050/cnhkbusapi2.2/rest/cl_basic_info/";
    private static final String signture = "userName="+"WHK00301api"+"&psw="+"1S3E8E49-D31C-0519B-3A16-7D4A04C623B5A";
    private static final String userName = "WHK00301api";


    /**
     * 线路查询
     * @return
     */
    public static Map<String,Object> getRoutes() throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "routes";
        String requestSignture = signture +"&timestamp="+timestamp;
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }

    /**
     * 站点查询
     * @param paramMap 接口入参
     * @return
     */
    public static Map<String,Object> getLocations(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "locations";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }


    /**
     * 班次查询
     * @param paramMap 接口入参
     * @return
     */
    public static Map<String,Object> getDeparturesZL(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "departuresZL";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }



    /**
     * 路线所有班次查询
     * @param paramMap 接口入参
     * @return
     */
    public static Map<String,Object> getBusLines(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "busLines";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }

    /**
     * 锁票-单程提交订单
     * @param paramMap 接口入参
     * @return
     */
    public static Map<String,Object> reservations(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "reservations";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }

    /**
     * 锁票确认
     * @param paramMap 接口入参
     * @return
     */
    public static Map<String,Object> getTickets(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "getTickets";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }


    /**
     * 取消锁票
     * @param paramMap 接口入参
     *        OrderNo 订单号
     * @return
     */
    public static Map<String,Object> cancelOrder(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "cancelOrder";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }

    /**
     * 车票状态查询
     * @param paramMap 接口入参
     *         OrderNo 订单号
     * @return
     */
    public static Map<String,Object> getOrderInfo(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "getOrderInfo";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }


    /**
     * 查询车票信息
     * @param paramMap 接口入参
     *        ticketNos 车票号
     * @return
     */
    public static Map<String,Object> getTicketInfo(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "getTicketInfo";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);
    }


    /**
     * 班次可售座位
     * @param paramMap 接口入参
     * @return
     */
    public static String getCanSaleSeats(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "getCanSaleSeats";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        return HttpUtils.httpget(requestUrl,paramMap);

    }


    /**
     * 查询可用的下站点
     * @param paramMap 接口入参
     * @return
     */
    public static Map<String,Object> getToStations(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "getToStations";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);

    }

    /**
     * 退票
     */
    public static Map<String,Object> refundTickets(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "refundTickets";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);

    }

    /**
     * 班次余票以及价格查询
     */
    public static Map<String,Object> getBcInfo(Map<String,String> paramMap) throws DocumentException {
        String timestamp = String.valueOf(new Date().getTime());
        String requestUrl = url + "getBcInfo";
        String requestSignture = signture +"&timestamp="+timestamp;
        paramMap.put("userName",userName);
        paramMap.put("timestamp",timestamp);
        paramMap.put("signture", MD5Util.stringMd5(requestSignture));
        String result = HttpUtils.httpget(requestUrl,paramMap);
        return ParseXmlUtil.parseXml(result);

    }
}
